package com.hzit.security.config;

import com.hzit.security.filter.HzitAuthenticationRightsFilter;
import com.hzit.security.filter.HzitLoginFilter;
import org.apache.catalina.startup.WebAnnotationSet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration;
import org.springframework.security.config.annotation.authentication.configuration.EnableGlobalAuthentication;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityCustomizer;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.security.web.authentication.www.BasicAuthenticationFilter;
import tk.mybatis.spring.annotation.MapperScan;

@Configuration
@MapperScan("com.hzit.security.mapper")
@EnableGlobalMethodSecurity(securedEnabled = true,prePostEnabled = true)
public class HzitSercurityConfig {
	@Autowired
	private AuthenticationEntryPoint authenticationEntryPoint;
	@Autowired
	private AccessDeniedHandler accessDeniedHandler;
	@Autowired
	private UserDetailsService userDetailsService;

	private AuthenticationManager authenticationManager;
	@Autowired
	private StringRedisTemplate redisTemplate;
	//
	// @Autowired
	// private HzitLoginFilter loginFilter;
	//1. 引入AuthenticationManager到spring容器中
	@Bean
	public AuthenticationManager authenticationManager(AuthenticationConfiguration configuration) throws Exception {
		authenticationManager =  configuration.getAuthenticationManager();
		return authenticationManager;
	}
	//2. 相关配置
	@Bean
	public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
		http.csrf().disable()
			.exceptionHandling()
				.accessDeniedHandler(accessDeniedHandler)                   // 处理未授权异常
				.authenticationEntryPoint(authenticationEntryPoint);        // 处理未认证异常

		// 因为使用了token机制，所以去掉原来的session保存SecurityContext的策略
		http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
		// 设置UserDetailService
		http.userDetailsService(userDetailsService);

		// 添加登录过滤器
		http.addFilter(new HzitLoginFilter(authenticationManager,redisTemplate));
		// 添加授权过滤器
		http.addFilter(new HzitAuthenticationRightsFilter(authenticationManager,redisTemplate));

		return http.build();
	}

	//3. 配置放行
	@Bean
	public WebSecurityCustomizer webSecurityCustomizer(){
		return web -> web.ignoring().mvcMatchers("/webjars/**");
	}

	//4. 配置密码加密器
	@Bean
	public PasswordEncoder passwordEncoder(){
		return new BCryptPasswordEncoder();
	}
}
